package com.woodws.fresh.im.service.impl

import com.woodws.fresh.common.core.domain.R
import com.woodws.fresh.common.security.util.SecurityContextUtils
import com.woodws.fresh.im.api.dto.ImInfo
import com.woodws.fresh.im.api.service.ImFriendService
import com.woodws.fresh.im.api.service.ImGroupService
import com.woodws.fresh.im.api.service.ImInfoService
import com.woodws.fresh.sys.api.service.SysUserService
import org.apache.dubbo.config.annotation.DubboReference
import org.apache.dubbo.config.annotation.DubboService
import org.springframework.stereotype.Service

/**
 * 基本信息表业务实现
 * @author maoxiaodong
 */
@DubboService
@Service
class ImInfoServiceImpl(val imFriendService: ImFriendService,
                        val imGroupService: ImGroupService
    ) : ImInfoService {

    @DubboReference
    private lateinit var sysUserService: SysUserService

    override fun findByLoginUser(): R<ImInfo> {
        val  user = SecurityContextUtils.user()
        val imInfo = ImInfo()
        imInfo.fiends = imFriendService.findByMyId(user.id!!).get()
        val ids: Array<Any> = imInfo.fiends.map{it.yourId as Any}.toTypedArray()
        val users = sysUserService.findByIds(*ids).get()
        imInfo.fiends.forEach { it.sysUserDto = users.find{user -> it.yourId == user.id} }
        imInfo.groups = imGroupService.findByUserId(user.id!!).get()
        return R.success(imInfo)
    }
}